package fr.umlv.escape_ig.editor;

import java.util.LinkedList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import fr.umlv.escape_ig.R;
import fr.umlv.escape_ig.convertion.Point;
import fr.umlv.escape_ig.manager.Display;

public class PrintBackground extends View{
	
	private double yScrolling;
	private double yScrolling2;
	private Bitmap backImg;
	private Bitmap backImg2;
	private Display display;
	private boolean ship1;
	private final LinkedList<Vessel> shipList;
	private GestureDetector myGestureDetector;
	private int currentHigth;
	private final int MINWHEIGTH;
	private DisplayMetrics ecran;
	
	private GestureDetector.SimpleOnGestureListener mScrollHandler = new GestureDetector.SimpleOnGestureListener(){

        @Override
        public boolean onDown(MotionEvent arg0) {
            return true;
        }
        @Override
        public boolean onDoubleTap(MotionEvent e){
        	float x=e.getX();
    		float y=e.getY();
    		if(!clicShip(x, y)){
    			if(ship1){
    				shipList.add(new Vessel(BitmapFactory.decodeResource(getResources(), R.drawable.ship), new Point(x,y)));
    			}
    			else{
    				shipList.add(new Vessel(BitmapFactory.decodeResource(getResources(), R.drawable.ship2), new Point(x,y)));
    			}
    			invalidate();
    		}
			return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        		moveMap((int) distanceY);
            return true;
        }
    };
    private boolean valideScroll(){
    	if(currentHigth>MINWHEIGTH)
    		return true;
    	return false;
    }
	private int valAbs(int nb){
		return nb<0?-nb:nb;
	}
	@SuppressWarnings("deprecation")
	public PrintBackground(Context context,DisplayMetrics ecran){
		
		super(context);
		myGestureDetector = new GestureDetector(mScrollHandler) ;
		this.ecran=ecran;
		display = new Display(context);
		backImg = BitmapFactory.decodeResource(getResources(), R.drawable.background);
		this.ship1=true;
		this.shipList = new LinkedList<Vessel>();
		yScrolling = -backImg.getHeight()+ecran.heightPixels;
		yScrolling2 = 2*backImg.getHeight();
		currentHigth = 0;
		MINWHEIGTH = 0;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawBitmap(backImg, 0, (float) yScrolling, null);
		display.diplayVesselBuilder(canvas);
		for(Vessel v : shipList){
			v.printVessel(canvas);
		}
	}
	private void moveMap(int scroll){
		yScrolling=yScrolling+scroll;
		for (Vessel v : shipList){
			v.setY(-scroll);
		}
		currentHigth=currentHigth+scroll;
		Toast.makeText(getContext(), "Cuurenthigth "+currentHigth+" scroll "+scroll+" yScrolling "+yScrolling, Toast.LENGTH_SHORT).show();
		invalidate();
	}
	@Override
	public boolean onTouchEvent(MotionEvent e) {
		return myGestureDetector.onTouchEvent(e);
		
	}

		
	private boolean clicShip(float x, float y){
		if(y<=display.getYShip2() && y>=0){
			if(x>=0 && x<=display.getXShip1()){
				this.ship1=true;
				return true;
			}else{
				this.ship1=false;
				return true;
			}
		}
		return false;
	}
}
